In [16]:
import os
import pickle
ls = files=os.listdir("Processed")
com = ls[0]
with open('Processed/'+com, 'rb') as f:
    data = pickle.load(f)

In [2]:
train_x = data['X'][0:2500]
test_x = data['X'][2500:]

In [3]:
train_y = data['Y'][0:2500]
test_y = data['Y'][2500:]

In [4]:
train_x = train_x.reshape([-1, 30, 10, 1])
test_x = test_x.reshape([-1, 30, 10, 1])

In [5]:
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d, avg_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import numpy as np
import tensorflow as tf
import os


curses is not supported on this machine (please install/reinstall curses for an optimal experience)

In [6]:
LR = 1e-4
MODEL_NAME = 'tt-{}-{}.model'.format(LR,'4conv-2fc-ind-'+com)#5conv-basic

In [9]:
tf.reset_default_graph()
convnet = input_data(shape=[None, 30, 10,1], name='input')

convnet = conv_2d(convnet, 64, 4, activation='relu') #--- 30 x 5
#convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 128, 3, activation='relu') #-----15 x 3
#convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 128, 3, activation='relu')
#convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 128, 2, activation='relu')
#convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 512, activation='softmax')
convnet = dropout(convnet, 0.70)

#convnet = fully_connected(convnet, 256, activation='softmax')

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_verbose=0)

In [226]:
model.load(MODEL_NAME)


INFO:tensorflow:Restoring parameters from C:\Users\Shachi Shah\Documents\Stocks\tt-0.0001-4conv-3fc-MTNL.BO.model

In [10]:
model.fit({'input': train_x},train_y, n_epoch=20, validation_set=({'input': test_x},test_y), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)


Training Step: 799  | total loss: 0.65310 | time: 3.223s
| Adam | epoch: 020 | loss: 0.65310 - acc: 0.6829 -- iter: 2496/2500
Training Step: 800  | total loss: 0.65610 | time: 4.315s
| Adam | epoch: 020 | loss: 0.65610 - acc: 0.6709 | val_loss: 0.65739 - val_acc: 0.6653 -- iter: 2500/2500
--

In [137]:
model.save(MODEL_NAME)


INFO:tensorflow:C:\Users\Shachi Shah\Documents\Stocks\tt-0.0001-4conv-3fc-MTNL.BO.model is not in all_model_checkpoint_paths. Manually adding it.

RNN


In [13]:
train_x = train_x.reshape([-1, 270, 1])
test_x = test_x.reshape([-1, 270,1])

In [17]:
tf.reset_default_graph()
RNN = input_data(shape=[None, 270, 1], name='input')

RNN = tflearn.lstm(RNN, 90, return_seq=True)

RNN = tflearn.lstm(RNN, 90)

RNN = tflearn.fully_connected(RNN, 2, activation='softmax')
RNN = dropout(RNN, 0.90)

RNN = tflearn.regression(RNN, optimizer='adam', loss='categorical_crossentropy', name="output")

model = tflearn.DNN(RNN, tensorboard_verbose=1)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
    362     try:
--> 363       xla_compile = op.get_attr("_XlaCompile")
    364       xla_separate_compiled_gradients = op.get_attr(

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in get_attr(self, name)
   1518       raise ValueError("No attr named '" + name + "' in " +
-> 1519                        str(self._node_def))
   1520     x = self._node_def.attr[name]

ValueError: No attr named '_XlaCompile' in name: "LSTM_1/LSTM_1/BasicLSTMCell_147/add"
op: "Add"
input: "LSTM_1/LSTM_1/BasicLSTMCell_147/Linear/MatMul"
input: "LSTM_1/LSTM_1/BasicLSTMCell/Linear/Bias/read"
attr {
  key: "T"
  value {
    type: DT_FLOAT
  }
}


During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-17-c1bdec795c87> in <module>()
     11 RNN = tflearn.regression(RNN, optimizer='adam', loss='categorical_crossentropy', name="output")
     12 
---> 13 model = tflearn.DNN(RNN, tensorboard_verbose=1)

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tflearn\models\dnn.py in __init__(self, network, clip_gradients, tensorboard_verbose, tensorboard_dir, checkpoint_path, best_checkpoint_path, max_checkpoints, session, best_val_accuracy)
     62                                max_checkpoints=max_checkpoints,
     63                                session=session,
---> 64                                best_val_accuracy=best_val_accuracy)
     65         self.session = self.trainer.session
     66 

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tflearn\helpers\trainer.py in __init__(self, train_ops, graph, clip_gradients, tensorboard_dir, tensorboard_verbose, checkpoint_path, best_checkpoint_path, max_checkpoints, keep_checkpoint_every_n_hours, random_seed, session, best_val_accuracy)
    129                 train_op.initialize_training_ops(i, self.session,
    130                                                  tensorboard_verbose,
--> 131                                                  clip_gradients)
    132 
    133             # Saver for saving a model

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tflearn\helpers\trainer.py in initialize_training_ops(self, i, session, tensorboard_verbose, clip_gradients)
    656             # Compute gradients operations
    657             with tf.control_dependencies([loss_avg_op, acc_avg_op]):
--> 658                 self.grad = tf.gradients(total_loss, self.train_vars)
    659                 if clip_gradients > 0.0:
    660                     self.grad, self.grad_norm = \

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method)
    558                 # functions.
    559                 in_grads = _MaybeCompile(
--> 560                     grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
    561               else:
    562                 # For function call ops, we add a 'SymbolicGradient'

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
    366       xla_scope = op.get_attr("_XlaScope").decode()
    367     except ValueError:
--> 368       return grad_fn()  # Exit early
    369 
    370   if not xla_compile:

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py in <lambda>()
    558                 # functions.
    559                 in_grads = _MaybeCompile(
--> 560                     grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
    561               else:
    562                 # For function call ops, we add a 'SymbolicGradient'

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\math_grad.py in _AddGrad(op, grad)
    597   sy = array_ops.shape(y)
    598   rx, ry = gen_array_ops._broadcast_gradient_args(sx, sy)
--> 599   return (array_ops.reshape(math_ops.reduce_sum(grad, rx), sx),
    600           array_ops.reshape(math_ops.reduce_sum(grad, ry), sy))
    601 

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py in reduce_sum(input_tensor, axis, keep_dims, name, reduction_indices)
   1234       _ReductionDims(input_tensor, axis, reduction_indices),
   1235       keep_dims,
-> 1236       name=name)
   1237 
   1238 

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in _sum(input, reduction_indices, keep_dims, name)
   2654   result = _op_def_lib.apply_op("Sum", input=input,
   2655                                 reduction_indices=reduction_indices,
-> 2656                                 keep_dims=keep_dims, name=name)
   2657   return result
   2658 

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    766         op = g.create_op(op_type_name, inputs, output_types, name=scope,
    767                          input_types=input_types, attrs=attr_protos,
--> 768                          op_def=op_def)
    769         if output_structure:
    770           outputs = op.outputs

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2336                     original_op=self._default_original_op, op_def=op_def)
   2337     if compute_shapes:
-> 2338       set_shapes_for_outputs(ret)
   2339     self._add_op(ret)
   2340     self._record_op_seen_by_control_dependencies(ret)

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in set_shapes_for_outputs(op)
   1717       shape_func = _call_cpp_shape_fn_and_require_op
   1718 
-> 1719   shapes = shape_func(op)
   1720   if shapes is None:
   1721     raise RuntimeError(

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in call_with_requiring(op)
   1667 
   1668   def call_with_requiring(op):
-> 1669     return call_cpp_shape_fn(op, require_shape_fn=True)
   1670 
   1671   _call_cpp_shape_fn_and_require_op = call_with_requiring

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py in call_cpp_shape_fn(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    608     res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
    609                                   input_tensors_as_shapes_needed,
--> 610                                   debug_python_shape_fn, require_shape_fn)
    611     if not isinstance(res, dict):
    612       # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

C:\Users\Shachi Shah\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
    669       output = pywrap_tensorflow.RunCppShapeInference(
    670           graph_def_version, node_def_str, input_shapes, input_tensors,
--> 671           input_tensors_as_shapes, status)
    672   except errors.InvalidArgumentError as err:
    673     if err.message.startswith("No shape inference function exists for op"):

KeyboardInterrupt: 

In [15]:
LR = 1e-4
MODEL_NAME = 'tt-{}-{}.model'.format(LR,'rnn-ind-'+com)#5conv-basic

In [ ]:
model.fit({'input': train_x},train_y, n_epoch=20, validation_set=({'input': test_x},test_y), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

Image Representation


In [11]:
def makeimage(d1_array):
    import numpy as np
    image=[]
    i=0
    while i<30:
        row=[]
        j=0
        while j<9:
            row.append(np.array([int(d1_array[i][j]*255/10000)]*3))
            j+=1
        image.append(np.array(row))
        i+=1
    image = np.array(image)
    return image

In [12]:
import matplotlib.pyplot as plt
image_counter=0

fig = plt.figure(figsize=(20,10))
while image_counter<125:
    
    Image = makeimage(data['X'][image_counter])

    Actual_Label = np.argmax(data['Y'][image_counter])
    
    y = fig.add_subplot(5,25,image_counter+1)
    y.imshow(Image)
    plt.title(Actual_Label)
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)
    image_counter+=1
plt.show()



In [ ]: